/**
 * Copyright (C), 2021-2022, 广州知铲科技有限公司
 * @ProjectName: android
 * @Package: com.zhichan.jdunion_plugin.helper
 * @ClassName: JdunionHelper
 * @Description:
 * @Author: frankylee
 * @CreateDate: 2022/9/13 12:04
 * @UpdateUser: frankylee
 * @UpdateData: 2022/9/13 12:04
 * @UpdateRemark: 更新说明
 */
package com.zhichan.jdunion_plugin.helper

import android.app.Activity
import android.util.Log
import com.kepler.jd.Listener.AsyncInitListener
import com.kepler.jd.Listener.IOaidCallBck
import com.kepler.jd.Listener.OpenAppAction
import com.kepler.jd.login.KeplerApiManager
import com.kepler.jd.sdk.bean.KelperTask
import com.kepler.jd.sdk.bean.KeplerAttachParameter
import java.lang.Exception
import io.flutter.plugin.common.MethodChannel.Result

class JdunionHelper {

    private var kelperTask: KelperTask? = null

    private val appKey = "76c965923ecd408cb8bbf4af07799b01"
    private val appSecret = "e421aa5c41884201b512107c41aca05d"

    private var activity: Activity? = null

    fun setActivity(activity: Activity?) {
        this.activity = activity
    }

    /**
     * 初始化
     */
    fun init(version: String) {
        KeplerApiManager.asyncInitSdk(activity?.application, appKey, appSecret, "", IOaidCallBck {
            return@IOaidCallBck "";
        }, object : AsyncInitListener {
            override fun onSuccess() {
                Log.e("Jdunion", "Kepler asyncInitSdk onSuccess!")
            }

            override fun onFailure() {
                Log.e("Jdunion", "Kepler asyncInitSdk 授权失败，请检查lib 工程资源引用；包名,签名证书是否和注册一致")
            }
        })
    }

    /**
     * 呼起京东APP，任意URl跳转
     */
    fun openJDAppByUrl(url: String, result: Result) {
        val attachParameter = KeplerAttachParameter()
        try {
            kelperTask = KeplerApiManager.getWebViewService()
                .openAppWebViewPage(
                    activity, url, attachParameter
                ) { p0, p1 ->
                    when (p0) {
                        OpenAppAction.OpenAppAction_start -> result.success(0)
                        OpenAppAction.OpenAppAction_result_NoJDAPP -> result.error(
                            p0.toString(),
                            "未安装京东App",
                            null
                        )
                        else -> kelperTask = null
                    }
                }
        } catch (e: Exception) {
            result.error(
                "-1",
                "打开京东App失败",
                null
            )
        }
    }

    /**
     * 呼起京东APP，任意URl跳转
     */
    fun openJXAppByUrl(url: String, result: Result) {
        val attachParameter = KeplerAttachParameter()
        try {
            kelperTask = KeplerApiManager.getWebViewService().openAppWebViewPageJX(activity, url, attachParameter) {
                p0, p1 ->
                when (p0) {
                    OpenAppAction.OpenAppAction_start -> result.success(0)
                    OpenAppAction.OpenAppAction_result_NoJDAPP -> result.error(
                        p0.toString(),
                        "未安装京喜App",
                        null
                    )
                    else -> kelperTask = null
                }
            }
        } catch (e: Exception) {
            result.error(
                "-1",
                "打开京东App失败",
                null
            )
        }
    }


}